<template>
  <div>
    <h1>类型1</h1>
    <!--Math.ceil 地板函数向上取整，把data1每三个数据一行拆分为N行-->
    <!--data1.slice 取data1从（0~3）的数据遍历，依次（3~6）的数组遍历-start<= index <end.....-->
    <!--el-row 每增加一行，内部el-col 生成3个div-->
    <el-row v-for="num in Math.ceil(data1.length /3)" :key="num">
      <el-col v-for="item1 in data1.slice((num-1)*3, num*3)" :key="item1.id" :span="8">
        <div :id="item1.key" class="ehart3" :span="8">
        </div>
      </el-col>
    </el-row>
    <h1>类型2</h1>
    <el-row v-for="num in Math.ceil(data2.length /3)" :key="num">
      <el-col v-for="item2 in data2.slice((num-1)*3, num*3)" :key="item2.id" :span="8">
        <div :id="item2.key" class="ehart3" :span="8">
        </div>
      </el-col>
    </el-row>
    <h1>类型3</h1>
    <el-row v-for="num in Math.ceil(data3.length /3)" :key="num">
      <el-col v-for="item3 in data3.slice((num-1)*3, num*3)" :key="item3.id" :span="8">
        <div :id="item3.key" class="ehart3" :span="8">
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import * as echarts from 'echarts'

export default {
  data() {
    return {
      data1: [],
      data2: [],
      data3: [],
      dataAll: []
    }
  },
  created() {
  },
  mounted() {
    this.initData()
  },
  methods: {
    initData() {
      this.axios.create({baseURL: '/api'}).get('/echart/echart3').then(({data}) => {
        if (data.code === 1) {
          this.dataAll = data.dataList
          this.data1 = this.dataAll.filter(item => item.are === 1)
          this.data2 = this.dataAll.filter(item => item.are === 2)
          this.data3 = this.dataAll.filter(item => item.are === 3)
          this.$nextTick(() => {
            this.statEchart()
          })
        } else {
          console.info('error')
        }
      })
    },
    statEchart() {
      for (let i = 0; i < this.dataAll.length; i++) {
        const thisData = this.dataAll[i]
        const subData = thisData.subList
        const id = thisData.key
        if (thisData.type === 1) {
          this.initChart1(id, thisData)
        } else if (thisData.type === 2) {
          thisData.category = subData.map(item => item.name).join(',').split(',')
          thisData.echartsData = subData.map(item => item.value).join(',').split(',')
          this.initChart2(id, thisData)
        } else if (thisData.type === 3) {
          thisData.category = subData.map(item => item.name).join(',').split(',')
          thisData.echartsData = subData.map(item => item.value).join(',').split(',')
          this.initChart3(id, thisData)
        }
      }
    },
    initChart1(id, data) {
      var chartDom = document.getElementById(id)
      var myChart = echarts.init(chartDom)
      var option

      option = {
        title: {
          text: data.title,
          subtext: 'Fake Data',
          left: 'center'
        },
        tooltip: {
          trigger: 'item'
        },
        legend: {
          orient: 'vertical',
          left: 'left'
        },
        series: [
          {
            name: 'Access From',
            type: 'pie',
            radius: '50%',
            data: data.subList,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      }

      option && myChart.setOption(option)
    },
    initChart2(id, data) {
      var chartDom = document.getElementById(id)
      var myChart = echarts.init(chartDom)
      var option

      option = {
        title: {
          text: data.title
        },
        xAxis: {
          type: 'category',
          data: data.category
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: data.echartsData,
            type: 'bar'
          }
        ]
      }

      option && myChart.setOption(option)
    },
    initChart3(id, data) {
      const chartDom = document.getElementById(id)
      const myChart = echarts.init(chartDom)
      var option

      option = {
        title: {
          text: data.title
        },
        xAxis: {
          type: 'category',
          data: data.category
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: data.echartsData,
            type: 'line'
          }
        ]
      }

      option && myChart.setOption(option)
    }
  }
}
</script>

<style scoped>
.ehart3 {
  width: 600px;
  height: 600px;
}

</style>
